package com.app.service.impl;

import com.app.mapper.AttractInvestmentMapper;
import com.app.mapper.DepartmentMapper;
import com.app.mapper.PlanBusinessMapper;
import com.app.model.*;
import com.app.service.AttractInvestmentService;
import com.app.service.ProjectProgressService;
import com.app.vo.AttractVo;
import com.app.vo.BusinessPlanVo;
import com.app.vo.FundsPlanVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class AttractInvestmentServiceImpl extends ServiceImpl<AttractInvestmentMapper, BusinessModel> implements AttractInvestmentService {
    @Autowired
    private AttractInvestmentMapper attractInvestmentMapper;
    @Autowired
    private PlanBusinessMapper planBusinessMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ProjectProgressService projectProgressService;

    @Override
    public BusinessPlanVo AttractList(PlanBusinessModel model) {
        List<PlanBusinessModel> model1 = planBusinessMapper.selectPlan(model);
        List<DepartmentModel> departmentModels = departmentMapper.selectType();
        for (int i = 0; i < departmentModels.size(); i++) {
            for (int i1 = 0; i1 < model1.size(); i1++) {
                if (model1.get(i1).getDepartmentId() == departmentModels.get(i).getId()) {
                    model1.get(i1).setType(departmentModels.get(i).getType());
                }
            }
        }
        List<SystemModel> departmentClass = departmentMapper.getDepartmentClass();
        for (int i = 0; i < departmentClass.size(); i++) {
            for (int i1 = 0; i1 < model1.size(); i1++) {
                if (model1.get(i1).getType() == departmentClass.get(i).getId()) {
                    model1.get(i1).setTypeText(departmentClass.get(i).getName());
                }
            }
        }
        for (int i = 0; i < model1.size(); i++) {
            Double sum = sum(model1.get(i).getDepartmentId(), model1.get(i).getYear());
            if (sum == null) {
                sum = 0.0;
            }
            model1.get(i).setCompletionFunds(sum);
            Double taskPlan = model1.get(i).getTaskPlan();
            double v = (double) sum / taskPlan;
            model1.get(i).setV(v);
            Double s = (double) sum / taskPlan * 100;
            String format = String.format("%.2f", s);
            model1.get(i).setTaskCompletion(Double.valueOf(format));
            if (v < 0.2) {
                model1.get(i).setStatus(65);
                model1.get(i).setStatusText("红灯单位");
            }
            if (v >= 0.2 && v < 0.9) {
                model1.get(i).setStatus(64);
                model1.get(i).setStatusText("黄灯单位");
            }
            if (v >= 0.9) {
                model1.get(i).setStatus(63);
                model1.get(i).setStatusText("蓝灯单位");
            }
        }
        int count = selectPlanCount(model);
        BusinessPlanVo vo = new BusinessPlanVo();
        List<PlanBusinessModel> model2 = new ArrayList<>();
        switch (model.getStatusText()) {
            case "全部":
                for (int i = 0; i < model1.size(); i++) {
                    model2.add(i, model1.get(i));
                }
                vo.setCount(count);
                break;
            case "红灯单位":
                int j = 0;
                for (int i = 0; i < model1.size(); i++) {
                    if (model1.get(i).getStatusText().equals("红灯单位")) {
                        model2.add(j, model1.get(i));
                        j++;
                    }
                }
                vo.setCount(j);
                break;
            case "黄灯单位":
                int a = 0;
                for (int i = 0; i < model1.size(); i++) {
                    if (model1.get(i).getStatusText().equals("黄灯单位") && model1.get(i).getStatus() != null) {
                        model2.add(a, model1.get(i));
                        a++;
                    }
                }
                vo.setCount(a);
                break;
            case "蓝灯单位":
                int b = 0;
                for (int i = 0; i < model1.size(); i++) {
                    if (model1.get(i).getStatusText().equals("蓝灯单位")) {
                        model2.add(b, model1.get(i));
                        b++;
                    }
                }
                vo.setCount(b);
                break;
        }
        if (model.getSort() == 0) {
            maoPao(model2);
        }
        vo.setList1(model2);
        return vo;
    }

    @Override
    public List<BusinessModel> getListByModel(BusinessModel model) {
        return attractInvestmentMapper.getListByModel(model);
    }

    @Override
    public int getCountByModel(BusinessModel model) {
        return attractInvestmentMapper.getCountByModel(model);
    }


    @Override
    public void add(BusinessModel model) {
        attractInvestmentMapper.insert(model);
    }

    @Override
    public void updateFunds(BusinessModel model) {
        attractInvestmentMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        attractInvestmentMapper.deleteById(id);
    }

    @Override
    public AttractVo selectCompletion(BusinessModel model) {

        List<BusinessModel> businessModels = attractInvestmentMapper.selectCompletion(model);
        int i = selectCompletionCount(model);
        AttractVo vo = new AttractVo();
        vo.setList(businessModels);
        vo.setCount(i);
        return vo;
    }

    private int selectCompletionCount(BusinessModel model) {
        return attractInvestmentMapper.selectCompletionCount(model);
    }

    @Override
    public AttractVo selectCompletionByTime(BusinessModel model) {
        List<BusinessModel> businessModels = attractInvestmentMapper.selectCompletionByTime(model);
        int i = selectCompletionByTimeCount(model);
        AttractVo vo = new AttractVo();
        vo.setList(businessModels);
        vo.setCount(i);
        return vo;
    }

    @Override
    public void savePlan(PlanBusinessModel model) {
        List<PlanBusinessModel> select = planBusinessMapper.select(model.getDepartmentId());
        if (model.getTaskPlan() == 0) {
            QueryWrapper<PlanBusinessModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("department_id", model.getDepartmentId());
            planBusinessMapper.delete(queryWrapper);
        }
        if (select.size() > 0) {
            planBusinessMapper.updatePlan(model);
        } else {
            int departmentId = model.getDepartmentId();
            Double taskPlan = model.getTaskPlan();
            String year = model.getYear();
            planBusinessMapper.insertPlan(taskPlan, year, departmentId);
        }
    }

    @Override
    public void addPlan(PlanBusinessModel model) {
        planBusinessMapper.insert(model);
    }


    @Override
    public BusinessPlanVo selectPlan(PlanBusinessModel model) {
        List<PlanBusinessModel> model1 = planBusinessMapper.selectPlan(model);
        for (int i = 0; i < model1.size(); i++) {
            Double sum = sum(model1.get(i).getDepartmentId(), model1.get(i).getYear());
            if (sum == null) {
                sum = 0.0;
            }
            model1.get(i).setCompletionFunds(sum);
            Double taskPlan = model1.get(i).getTaskPlan();
            Double v = sum / taskPlan;
            model1.get(i).setV(v);
            Double s = (double) sum / taskPlan * 100;
            String format = String.format("%.2f", s);
            model1.get(i).setRate(format);
            if (v < 0.2) {
                model1.get(i).setStatus(65);
                model1.get(i).setStatusText("红灯单位");
            }
            if (v >= 0.2 && v < 0.9) {
                model1.get(i).setStatus(64);
                model1.get(i).setStatusText("黄灯单位");
            }
            if (v >= 0.9) {
                model1.get(i).setStatus(63);
                model1.get(i).setStatusText("蓝灯单位");
            }
        }
        List<DepartmentModel> departmentModels = departmentMapper.get();
        for (int i = 0; i < departmentModels.size(); i++) {
            departmentModels.get(i).setDepartmentId(departmentModels.get(i).getId());
        }
        for (int i = 0; i < departmentModels.size(); i++) {
            for (int j = 0; j < model1.size(); j++) {
                if (departmentModels.get(i).getId() == model1.get(j).getDepartmentId()) {
                    departmentModels.get(i).setTaskPlan(model1.get(j).getTaskPlan());
                    departmentModels.get(i).setCompletionFunds(model1.get(j).getCompletionFunds());
                    departmentModels.get(i).setRate(model1.get(j).getRate());
                    departmentModels.get(i).setStatusText(model1.get(j).getStatusText());
                    departmentModels.get(i).setStatus(model1.get(j).getStatus());
                }
            }
        }
        int i = selectPlanCount(model);
        BusinessPlanVo vo = new BusinessPlanVo();
        vo.setList(departmentModels);
        vo.setCount(i);
        return vo;
    }

    @Override
    public AnswerModel selectCount() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        PlanBusinessModel model = new PlanBusinessModel();
        model.setYear(year);
        List<PlanBusinessModel> model1 = planBusinessMapper.selectPlan(model);
        for (int i = 0; i < model1.size(); i++) {
            Double sum = sum(model1.get(i).getDepartmentId(), model1.get(i).getYear());
            if (sum == null) {
                sum = 0.0;
            }
            model1.get(i).setCompletionFunds(sum);
            Double taskPlan = model1.get(i).getTaskPlan();
            Double v = sum / taskPlan;
            model1.get(i).setV(v);
            Double s = (double) sum / taskPlan * 100;
            String format = String.format("%.2f", s);
            model1.get(i).setRate(format);
            if (v < 0.2) {
                model1.get(i).setStatus(65);
                model1.get(i).setStatusText("红灯单位");
            }
            if (v >= 0.2 && v < 0.9) {
                model1.get(i).setStatus(64);
                model1.get(i).setStatusText("黄灯单位");
            }
            if (v >= 0.9) {
                model1.get(i).setStatus(63);
                model1.get(i).setStatusText("蓝灯单位");
            }
        }
        int redCount = 0;
        int blueCount = 0;
        int yellowCount = 0;
        for (int i = 0; i < model1.size(); i++) {
            if (model1.get(i).getStatus() == 65) {
                redCount++;
            }
            if (model1.get(i).getStatus() == 63) {
                blueCount++;
            }
            if (model1.get(i).getStatus() == 64) {
                yellowCount++;
            }
        }
        AnswerModel model2 = new AnswerModel();
        model2.setBlueCount(blueCount);
        model2.setRedCount(redCount);
        model2.setYellowCount(yellowCount);
        return model2;
    }

    @Override
    public BusinessModel selectOne() {
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        QueryWrapper<BusinessModel> businessqueryWrapper = new QueryWrapper<>();
        businessqueryWrapper.select("sum(amount) as sumAll");
        businessqueryWrapper.eq("year", year);
        return attractInvestmentMapper.selectOne(businessqueryWrapper);
    }

    @Override
    public List<PlanBusinessModel> getBusinessTable() {
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        //获取单位名称和年度计划
        List<PlanBusinessModel> fund = planBusinessMapper.getBusinessTable(year);
        List<DepartmentModel> departmentList = projectProgressService.getDepartmentList();
        if (departmentList.size() != 0) {
            for (int i = 0; i < departmentList.size(); i++) {
                for (int i1 = 0; i1 < fund.size(); i1++) {
                    if (fund.get(i1).getDepartmentId() == departmentList.get(i).getId()) {
                        fund.get(i1).setDepartmentText(departmentList.get(i).getUnitName());
                    }
                }
            }
        }
        //获取sumAll资金总额
        for (int i = 0; i < fund.size(); i++) {
            int departmentId = fund.get(i).getDepartmentId();
            QueryWrapper<BusinessModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("sum(amount) as sumAll");
            queryWrapper.eq("year", year);
            queryWrapper.eq("department_id", departmentId);
            BusinessModel model = attractInvestmentMapper.selectOne(queryWrapper);
            if (model == null) {
//                fund.remove(model.getDepartmentId());
                continue;
            }
            Double sumAll = model.getSumAll();
            fund.get(i).setSumAll(sumAll);
        }
        // sumCount资金笔数
        for (int i = 0; i < fund.size(); i++) {
            int departmentId = fund.get(i).getDepartmentId();
            QueryWrapper<BusinessModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("count(department_id) as sumCount");
            queryWrapper.eq("year", year);
            queryWrapper.eq("department_id", departmentId);
            BusinessModel model = attractInvestmentMapper.selectOne(queryWrapper);
            if (model == null) {
//                fund.remove(model.getDepartmentId());
                continue;
            }
            double sumCount = model.getSumCount();
            fund.get(i).setSumCount(sumCount);
        }
        //rate百分比
        for (int i = 0; i < fund.size(); i++) {
            Double taskPlan = fund.get(i).getTaskPlan();
            double sumAll = fund.get(i).getSumAll();
            String format = String.format("%.2f", sumAll / taskPlan);
            fund.get(i).setRate(format);
        }
        return fund;
    }

    @Override
    public List<BusinessModel> getList() {
        return attractInvestmentMapper.getList();
    }

    private Double sum(int departmentId, String year) {
        return attractInvestmentMapper.sum(departmentId, year);
    }

    private int selectPlanCount(PlanBusinessModel model) {
        return planBusinessMapper.selectPlanCount(model);
    }

    private int selectCompletionByTimeCount(BusinessModel model) {
        return attractInvestmentMapper.selectCompletionByTimeCount(model);
    }


    private List<PlanBusinessModel> maoPao(List<PlanBusinessModel> model) {
        List<PlanBusinessModel> modelList = new ArrayList<>();
        for (int i = 0; i < model.size() - 1; i++) {
            for (int j = 0; j < model.size() - 1 - i; j++) {
                if (model.get(i).getStatusText() == null) {
                    return null;
                }
                if (model.get(j).getV() < model.get(j + 1).getV()) {
                    modelList.add(i, model.get(j));
                    model.set(j, model.get(j + 1));
                    model.set(j + 1, modelList.get(i));
                }
            }
        }
        return model;
    }
}
